using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Web.UI.WebControls;

using dotnetCHARTING;

namespace hxSec.StaticPage
{
    /// <summary>
    /// 画折线图
    /// </summary>
    public class clsLineChrt
    {
        #region Fields
        // 图形数据源
        private DataSet _data;

        private int _width;         // 图形宽度
        private int _height;        // 图形高度 
        private string _filename;   // 图形文件名称
        private string _account;    // 客户号
        private string _format;     // 图形格式
        private string _type;       // 图形分类

        #endregion

        #region Contructors
        public clsLineChrt(int iWidth, int iHeight, string strAccount, string strFormat, string strType, DataSet dtData)
        {
            Data = dtData;

            Width = iWidth;
            Height = iHeight;
            Account = strAccount;
            ImgFormat = strFormat;
            Type = strType;
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// 图形数据源
        /// </summary>
        public DataSet Data
        {
            get { return _data; }
            set { _data = value; }
        }

        /// <summary>
        /// 图形宽度
        /// </summary>
        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        /// <summary>
        /// 图形高度
        /// </summary>
        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        /// <summary>
        /// 图形文件名称
        /// </summary>
        public string FileName
        {
            get { return _filename; }
            set { _filename = value; }
        }

        /// <summary>
        /// 客户号
        /// </summary>
        public string Account
        {
            get { return _account; }
            set { _account = value; }
        }

        /// <summary>
        /// 图形格式
        /// </summary>
        public string ImgFormat
        {
            get { return _format; }
            set { _format = value; }
        }

        /// <summary>
        /// 图形格式
        /// </summary>
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }
        #endregion

        #region DrawChart
        public byte[] DrawLineChrt()
        {
            if (Data == null || Data.Tables[0].Rows.Count == 0)
                return null;

            dotnetCHARTING.Chart lineChart = new dotnetCHARTING.Chart();

            // Set the title.
            lineChart.Title = "";
            lineChart.LegendBox.Position = LegendBoxPosition.BottomMiddle;
            lineChart.LegendBox.Template = "%PercentOfTotal%icon%name%";
            lineChart.LegendBox.Template = "%icon%name";
            lineChart.LegendBox.ClearColors();
            // Set 3D
            lineChart.Use3D = false;

            // Set the Chart1 Type
            lineChart.Type = ChartType.Combo;

            // Set default transparency
            //lineChart.DefaultSeries.DefaultElement.Transparency = 1;

            lineChart.ChartArea.Background.Color = Color.White;
            lineChart.ChartArea.Shadow.Color = Color.White;
            lineChart.ChartArea.YAxis.ZeroTick.GridLine.Color = Color.White;
            lineChart.YAxis.NumberPercision = 0;
            lineChart.YAxis.FormatString = "0.00";
            lineChart.YAxis.ZeroTick.Line.Color = Color.White;
            lineChart.XAxis.ShowGrid = true;
            lineChart.AutoNameLabels = true;

            // Set show value
            //lineChart.DefaultSeries.DefaultElement.ShowValue = true;
            //lineChart.YAxis.FormatString = "0.0";

            // Set labeling mode.
            //lineChart.PieLabelMode = PieLabelMode.Inside;

            // Set the directory where the images will be stored.
            lineChart.TempDirectory = "";

            // Set he chart size.
            lineChart.Width = Unit.Pixel(Width);
            lineChart.Height = Unit.Pixel(Height);

            // Set background
            //Chart1.ChartArea.Background.Color = Color.White;
            //lineChart.ChartArea.ClearColors();

            // Add the random data.
            lineChart.SeriesCollection.Add(getArrayData());

            Bitmap b = lineChart.GetChartBitmap();
            MemoryStream imgStream = new MemoryStream();

            switch (ImgFormat)
            {
                case "bmp":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Bmp);
                    break;
                case "jpg":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case "gif":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Gif);
                    break;
                case "png":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);
                    break;
                default:
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
            }


            return imgStream.ToArray();
        }

        private SeriesCollection getArrayData()
        {
            SeriesCollection SC = new SeriesCollection();
            for (int a = 0; a < Data.Tables[0].Rows.Count; a++)
            {
                Series sr = new Series();
                sr.Type = SeriesType.Line;
                sr.Line.Width = 1;
                sr.Name = Data.Tables[0].Rows[a]["STOCKNAME"].ToString();
                    
                for (int i = 0; i < Data.Tables[1].Rows.Count; i++)
                {
                    if (Data.Tables[0].Rows[a]["STOCKCODE"].Equals(Data.Tables[1].Rows[i]["NAME"]))
                    {
                        Element e = new Element();
                        e.Marker.Visible = false;
                        e.Name = Convert.ToDateTime(Data.Tables[1].Rows[i]["DATE"]).ToString("MM-dd");
                        e.YValue = Convert.ToDouble(Data.Tables[1].Rows[i]["VALUE"]);
                        sr.Elements.Add(e);
                    }                  
                }
                SC.Add(sr);   
            }
            return SC;
        }

        #endregion
    }
}
